package com.fh.modules.ems.controller;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.fh.common.api.vo.Result;
import com.fh.common.aspect.annotation.AutoLog;
import com.fh.common.constant.CommonConstant;
import com.fh.common.query.QueryWrapperUtil;
import com.fh.modules.ems.entity.EmsShop;
import com.fh.modules.ems.service.IEmsShopService;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;

 /**
 * @Title: Controller
 * @Description: 商户门店管理
 * @author： code generator
 * @date：   2019-06-02
 * @version： V1.0
 */
@RestController
@Slf4j
@Api("商户门店管理")
public class EmsShopController {
	@Autowired
	private IEmsShopService emsShopService;
	
	/**
	  * 分页列表查询
	 * @param emsShop
	 * @param pageNo
	 * @param pageSize
	 * @param req
	 * @return
	 */
	@GetMapping(value = "/ems/emsShop/list")
	@ApiOperation("分页列表查询")
	@AutoLog(value="分页查询门店",logType=CommonConstant.LOG_TYPE_QUERY)
	public Result queryPageList(EmsShop emsShop,
									  @RequestParam(name="pageNo", defaultValue="1") Integer pageNo,
									  @RequestParam(name="pageSize", defaultValue="10") Integer pageSize,
									  HttpServletRequest req) {
		QueryWrapper<EmsShop> queryWrapper = new QueryWrapper<EmsShop>();
		emsShop.setDelFlag(CommonConstant.DEL_FLAG_0);
		QueryWrapperUtil.initQueryWrapper(queryWrapper, emsShop);
		Page<EmsShop> page = new Page<EmsShop>(pageNo, pageSize);
		IPage<EmsShop> pageList = emsShopService.page(page, queryWrapper);
		return Result.ok(pageList);
	}
	
	/**
	  *   添加
	 * @param emsShop
	 * @return
	 */
	@PostMapping(value = "/ems/emsShop/add")
	@ApiOperation("添加")
	@AutoLog("商户门店添加")
	public Result add(@RequestBody EmsShop emsShop) {
		emsShop.setDelFlag(CommonConstant.DEL_FLAG_0);
		emsShop.setAccountCurrent(0);
		emsShopService.save(emsShop);
		return Result.ok("添加成功");
	}
	
	/**
	  *  编辑
	 * @param emsShop
	 * @return
	 */
	@PutMapping(value = "/ems/emsShop/edit")
	@ApiOperation("编辑")
	@AutoLog("商户门店修改")
	public Result edit(@RequestBody EmsShop emsShop) {
		boolean ok = emsShopService.updateById(emsShop);
		return ok?Result.ok("修改成功"):Result.error("修改失败");
	}

	 /**
	  *  商户门店详情
	  * @param id
	  * @return
	  */
	 @GetMapping(value = "/ems/emsShop/detail")
	 @ApiOperation("商户门店详情查询")
	 @AutoLog("商户门店详情查询")
	 public Result detail(@RequestParam(name="id") String id) {
		 EmsShop emsShop = emsShopService.getById(id);
		 return Result.ok(emsShop);
	 }
	
	/**
	  *  批量删除
	 * @param ids
	 * @return
	 */
	@DeleteMapping(value = "/ems/emsShop/deleteBatch")
	@ApiOperation("批量删除")
	@AutoLog("商户门店批量删除")
	public Result deleteBatch(@RequestParam(name="ids",required=true) String ids) {
		emsShopService.deleteBatchEntityByDelFlag(ids);
		return Result.ok("批量删除成功");
	}
	
}
